import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName:'login' */'@/views/login/index')
  },
  {
    path: '',
    name: '',
    meta: {
      requireAuth: true
    },
    component: () => import(/* webpackChunkName:'layout' */'@/views/layout/index'),
    children: [
      {
        path: '/',
        name: 'home',
        component: () => import(/* webpackChunkName: 'home' */'@/views/home/index')
      },
      {
        path: '/role',
        name: 'role',
        component: () => import(/* webpackChunkName: 'role' */'@/views/role/index')
      },
      {
        path: '/menu',
        name: 'menu',
        component: () => import(/* webpackChunkName: 'menu' */'@/views/menu/index')
      },
      {
        path: '/resource',
        name: 'resource',
        component: () => import(/* webpackChunkName: 'resource' */'@/views/resource/index')
      },
      {
        path: '/course',
        name: 'course',
        component: () => import(/* webpackChunkName: 'course' */'@/views/course/index')
      },
      {
        path: '/user',
        name: 'user',
        component: () => import(/* webpackChunkName:'user' */'@/views/user/index')
      },
      {
        path: '/menu/addMenu',
        name: 'addMenu',
        component: () => import(/* webpackChunkName: 'addMenu' */'@/views/menu/addMenu')
      },
      {
        path: '/menu/:id/updateMenu',
        name: 'updateMenu',
        component: () => import(/* webpackChunkName: 'updataMenu' */'@/views/menu/updateMenu')
      },
      {
        path: '/role/:roleId/alloc-menu',
        name: 'alloc-menu',
        component: () => import(/* webpackChunkName: 'allocMenu' */'@/views/role/allock-menu'),
        props: true
      },
      {
        path: '/role/:roleId/role-set',
        name: 'role-set',
        component: () => import(/* webpackChunkName: 'role-set' */'@/views/role/role-set'),
        props: true
      },
      {
        path: '/course/addCourse',
        name: 'add-course',
        component: () => import(/* webpackChunkName: 'addCourse' */'@/views/course/addCourse')
      },
      {
        path: '/course/:courseId/courseEdit',
        name: 'course-edit',
        component: () => import(/* webpackChunkName: 'courseEdit' */'@/views/course/courseEdit'),
        props: true
      },
      {
        path: '/course/:courseId/section',
        name: 'course-section',
        component: () => import(/* webpackChunkName: 'courseSection' */'@/views/course/section'),
        props: true
      },
      {
        path: '/course/:courseId/video',
        name: 'video',
        component: () => import(/* webpackChunkName: 'video' */'@/views/course/video'),
        props: true
      },
      {
        path: '/advert',
        name: 'advert',
        component: () => import(/* webpackChunkName: 'advert-list' */'@/views/advert/indexList')
      }
    ]
  },
  {
    path: '*',
    name: 'error',
    component: () => import(/* webpackChunkName:'error' */'@/views/error/index')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.matched.some(item => item.meta.requireAuth)) {
    if (!store.state.user) {
      return next({
        name: 'login',
        query: {
          redirct: to.fullPath
        }
      })
    }
    next()
  } else {
    next()
  }
})

export default router
